package git.soulbgm.common.filter;

import git.soulbgm.common.token.JwtToken;
import git.soulbgm.config.TokenConfig;
import git.soulbgm.pojo.PrincipalUser;
import git.soulbgm.utils.JsonUtil;
import git.soulbgm.utils.JwtUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 检测token过滤器
 *
 * @author SoulBGM
 * @version 1.0
 * @date 2019/11/6 16:32
 */
public class TokenFilter extends BasicHttpAuthenticationFilter {

    private TokenConfig tokenConfig;

    private StringRedisTemplate redisTemplate;

    public TokenFilter() {
    }

    public TokenFilter(TokenConfig tokenConfig, StringRedisTemplate redisTemplate) {
        this.tokenConfig = tokenConfig;
        this.redisTemplate = redisTemplate;
    }

    public void setTokenConfig(TokenConfig tokenConfig) {
        this.tokenConfig = tokenConfig;
    }

    public void setRedisTemplate(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 检测Header里Authorization字段
     * 判断是否登录
     *
     * @param request
     * @param response
     * @return
     */
    @Override
    protected boolean isLoginAttempt(ServletRequest request, ServletResponse response) {
        HttpServletRequest req = (HttpServletRequest) request;
        String authorization = req.getHeader(tokenConfig.getHeaderName());
        return authorization != null;
    }

    /**
     * 执行登录
     *
     * @param request
     * @param response
     * @return
     */
    @Override
    protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {
        return super.executeLogin(request, response);
    }

    /**
     * 生成Token
     *
     * @param request
     * @param response
     * @return
     */
    @Override
    protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) {
        HttpServletRequest req = (HttpServletRequest) request;
        String authorization = req.getHeader(tokenConfig.getHeaderName());
        return new JwtToken(authorization);
    }

    /**
     * 登录成功
     *
     * @param token
     * @param subject
     * @param request
     * @param response
     * @return
     */
    @Override
    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
        return true;
    }

    /**
     * 登录失败
     *
     * @param token
     * @param e
     * @param request
     * @param response
     * @return
     */
    @Override
    protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
        HttpServletResponse res = (HttpServletResponse) response;
        try {
            res.sendError(403, "令牌已过期或不正确");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return false;
    }

}
